package com.sjllb.tank;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 这个类的作用是坦克游戏的主窗口
 * 
 * @author lianlibo
 * 
 */

public class TankClient extends Frame {

	/**
	 * 整个坦克游戏窗口的宽度
	 */
	public static final int GAME_WIDTH = 800;
	/**
	 * 整个坦克游戏窗口的高度
	 */
	public static final int GAME_HEIGHT = 600;

	Tank myTank = new Tank(50, 50, true, Direction.STOP, this);
	Blood b = new Blood();
	Random r = new Random();

	List<Missile> missiles = new ArrayList<Missile>();
	List<Explode> explodes = new ArrayList<Explode>();
	List<Tank> tanks = new ArrayList<Tank>();

	Wall w1 = new Wall(100, 100, 20, 150, this);
	Wall w2 = new Wall(400, 200, 150, 20, this);

	Image offScreenImage = null;

	/**
	 * 如果敌方坦克为0的话，再画出5个敌人的坦克。
	 */
	public void paint(Graphics g) {

		if (this.tanks.size() <= 0) {
			for (int i = 0; i < 5; i++) {
				tanks.add(new Tank(50 + 60 * (i + 3), 500, false, Direction.D,
						this));
			}
		}

		myTank.draw(g);
		myTank.eat(b);
		// myTank.bug();

		/*
		 * 指明坦克-子弹-爆炸的数量以及主坦克的生命值
		 */
		g.drawString("missile count:" + missiles.size(), 10, 50);
		g.drawString("explode count:" + explodes.size(), 10, 70);
		g.drawString("tank count:" + tanks.size(), 10, 90);
		g.drawString("tank life:" + myTank.getLife(), 10, 110);

		// 画出missile中的子弹
		for (int i = 0; i < missiles.size(); i++) {
			Missile m = missiles.get(i);
			m.draw(g);
			m.hitTanks(tanks);
			m.hitTank(myTank);
			m.hitWall(w1);
			m.hitWall(w2);
			// if(!m.isLive()) missiles.remove(m);
			// else m.draw(g);
		}

		// 画出explodes中的爆炸
		for (int i = 0; i < explodes.size(); i++) {
			Explode e = explodes.get(i);
			e.draw(g);
		}

		// 画出tanks中的敌方坦克
		for (int i = 0; i < tanks.size(); i++) {
			Tank e = tanks.get(i);
			e.draw(g);
			e.collidesWall(w1);
			e.collidesWall(w2);
			e.colledesTanks(tanks);
		}
		w1.draw(g);
		w2.draw(g);
		b.draw(g);

	}

	// 双缓冲技术
	public void update(Graphics g) {
		if (offScreenImage == null) {
			offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
		}
		Graphics goffScreen = offScreenImage.getGraphics();
		Color c = goffScreen.getColor();
		goffScreen.setColor(Color.GREEN);
		goffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
		goffScreen.setColor(c);
		paint(goffScreen);
		g.drawImage(offScreenImage, 0, 0, null);
	}

	/**
	 * 本方法显示游戏的主窗口
	 */

	public void lauchFrame() {

		// 画出敌方10个坦克
		for (int i = 0; i < 10; i++) {
			tanks.add(new Tank(50 + 40 * (i + 3), 300, false, Direction.D, this));
		}

		this.setLocation(400, 300);
		this.setSize(GAME_WIDTH, GAME_HEIGHT);
		this.setTitle("TankWar");
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		this.setBackground(Color.GREEN);
		this.setResizable(false);
		this.addKeyListener(new KeyMonitor());
		this.setVisible(true);

		new Thread(new PaintThread()).start();
	}

	private class PaintThread implements Runnable {

		public void run() {
			while (true) {
				repaint();// 先调用update(),再调用paint()
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}

	}

	private class KeyMonitor extends KeyAdapter {

		public void keyReleased(KeyEvent e) {
			myTank.keyReleased(e);
		}

		public void keyPressed(KeyEvent e) {
			myTank.keyPressed(e);
		}
	}

	/**
	 * 程序入口
	 * 
	 * @param args
	 *            字符串数组
	 */
	public static void main(String[] args) {
		TankClient tc = new TankClient();
		tc.lauchFrame();

	}

}
